package com.shopping.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DB {

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	
	private DB(){
		
		
		
	}
	
	
	public static Connection getConnect(){
		Connection conn= null;
		try{
			String url = "jdbc:mysql://localhost:3306/shopping";
			String user  = "root";
			String password = "123456";
			conn = DriverManager.getConnection(url, user, password);
		}catch(SQLException e){
			e.printStackTrace();
		}
		return conn;
		
	}
	
	
	
	public static PreparedStatement getPrepareStatement(Connection connect,String sql){
		PreparedStatement pstmt = null;
		try {
			pstmt = connect.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pstmt;
	}
	
	
	public static void closStatement(PreparedStatement pstmt){
		if(pstmt!=null){
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void closConn(Connection connect){
		if(connect!=null){
			try {
				connect.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	public static ResultSet executeQuery(Connection connect , String sql){
		ResultSet rs = null;
		try{
			
			PreparedStatement pstmt = DB.getPrepareStatement(connect,sql);	
			
			rs = pstmt.executeQuery();
			
			
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public static void closResultSet(ResultSet rs){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}
